<template>
  <div class="header">
    <el-container>
      <el-header>
        <el-row>
          <el-col class="logo" :span="3">
            <a href="/">
              <img src="../../static/image/logo.svg" alt="" />
            </a>
          </el-col>
          <ul>
              <li v-for="nav in nav_list" :key="nav.id" class="nav full-left">
                <span v-if="nav.is_site"><a :href="nav.link">{{nav.title}}</a></span>
                <span v-else><router-link :to="nav.link">{{nav.title}}</router-link></span>
              </li>
          </ul>
          <!-- <el-col class="nav" :span="16">
            <el-row>
              <el-col :span="3"
                ><router-link class="current" to="/course"
                  >免费课</router-link
                ></el-col
              >
              <el-col :span="3"><router-link to="/">轻课</router-link></el-col>
              <el-col :span="3"
                ><router-link to="/">学位课</router-link></el-col
              >
              <el-col :span="3"><router-link to="/">题库</router-link></el-col>
              <el-col :span="3"><router-link to="/">教育</router-link></el-col>
            </el-row>
          </el-col> -->
          <el-col class="login-bar full-right" :span="5">
            <el-row v-if="token">
              <el-col class="cart-ico" :span="9">
                <router-link to="">
                  <b class="goods-number">{{$store.state.cart_lenght}}</b>
                  <img
                    class="cart-icon"
                    src="../../static/image/cart.svg"
                    alt=""
                  />
                  <span><router-link to="/cart">购物车</router-link></span>
                </router-link>
              </el-col>
              <el-col class="study" :span="8" :offset="2"
                ><router-link to="">学习中心</router-link></el-col
              >
              <el-col class="member" :span="5">
                <el-menu class="el-menu-demo" mode="horizontal">
                  <el-submenu index="2">
                    <template slot="title"
                      ><router-link to=""
                        ><img
                          src="../../static/image/logo@2x.png"
                          alt="" /></router-link
                    ></template>
                    <el-menu-item index="2-1">我的账户</el-menu-item>
                    <el-menu-item index="2-2">我的订单</el-menu-item>
                    <el-menu-item index="2-3">我的优惠卷</el-menu-item>
                    <el-menu-item index="2-3" @click="logoutHander"
                      >退出登录</el-menu-item
                    >
                  </el-submenu>
                </el-menu>
              </el-col>
            </el-row>
            <el-row v-else>
              <el-col class="cart-ico" :span="9">
                <router-link to="">
                  <img class="cart-icon" src="../../static/image/cart.svg" alt=""/>
                  <span><router-link to="/cart">购物车</router-link></span>
                </router-link>
              </el-col>
              <el-col :span="10" :offset="5">
                <span class="register">
                  <router-link to="/user/login">登录</router-link>
                  &nbsp;&nbsp;|&nbsp;&nbsp;
                  <router-link to="/register">注册</router-link>
                </span>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-header>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      token: "",
      nav_list: [],
      // carts_lenght:0,
    };
  },
  created() {
    this.check_user_login();
    this.get_nav();
    // this.lenght();
  },
  methods: {
    // lenght(){
    //   // console.log("===",this.$store.state.cart_lenght);
    //   this.carts_lenght = this.$store.state.cart_lenght
    // },
    check_user_login() {
      // 获取用户的登录状态
      // console.log(sessionStorage.token);
      // console.log(localStorage.token);
      this.token = sessionStorage.user_token || localStorage.user_token;
      // console.log(this.token);
      return this.token;
    },
    get_nav() {
      this.$axios.get("home/nav/header/", {})
        .then((response) => {
          this.nav_list = response.data;
        })
        .catch((error) => {
          console.log(error.response);
        });
    },
    logoutHander() {
      // 退出登录
      localStorage.removeItem("user_token");
      localStorage.removeItem("user_id");
      localStorage.removeItem("user_name");
      sessionStorage.removeItem("user_token");
      sessionStorage.removeItem("user_id");
      sessionStorage.removeItem("user_name");
      this.check_user_login();
    },
  },
};
</script>

<style scoped>
.header {
  top: 0;
  left: 0;
  right: 0;
  margin: auto;
  background-color: #fff;
  height: 80px;
  z-index: 1000;
  position: fixed;
  box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
}
.header .el-container {
  width: 1200px;
  margin: 0 auto;
}
.el-header {
  height: 80px !important;
  padding: 0;
}
/* .logo{

  } */
.logo img {
  padding-top: 22px;
}

.nav {
  margin-top: 30px;
  padding-left: 60px;
}

.nav .el-col a {
  display: inline-block;
  text-align: center;
  padding-bottom: 16px;
  padding-left: 5px;
  padding-right: 5px;
  position: relative;
  font-size: 16px;
  margin-left: 20px;
}

.nav .el-col .current {
  color: #4a4a4a;
  border-bottom: 4px solid #ffc210;
}

.login-bar {
  margin-top: 22px;
}
.cart-ico {
  position: relative;
  border-radius: 17px;
}
.cart-ico:hover {
  background: #f0f0f0;
}
.goods-number {
  width: 16px;
  height: 16px;
  line-height: 17px;
  font-size: 12px;
  color: #fff;
  text-align: center;
  background: #fa6240;
  border-radius: 50%;
  transform: scale(0.8);
  position: absolute;
  left: 16px;
  top: -1px;
}
.cart-icon {
  width: 15px;
  height: auto;
  margin-left: 6px;
}
.cart-ico span {
  margin-left: 12px;
}
.member img {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: inline-block;
}
.member img:hover {
  border: 1px solid yellow;
}

</style>
